//
//  BCNewOrdDetailVC.m
//  BangcaiSale
//
//  Created by lsy on 16/7/7.
//  Copyright © 2016年 Bangcai. All rights reserved.
//

#import "BCNewOrdDetailVC.h"
#import "BCNewOrderDetailInfoCollectionViewCell.h"
#import "BCGaryNullCollectionViewCell.h"
#import "BCNewOrderStuffTitleCollectionViewCell.h"
#import "BCNewOrderStuffCollectionViewCell.h"

@interface BCNewOrdDetailVC () <UICollectionViewDelegate,UICollectionViewDataSource>
//主视图
@property (nonatomic, strong) UICollectionView *mainCollectionView;
@end

@implementation BCNewOrdDetailVC

-(void)viewDidLoad {
    [super viewDidLoad];
    [self p_initNavgationBar];
    [self p_initCollectionView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //隐藏导航栏
    self.navigationController.navigationBar.hidden = NO;
    
    if (!self.rdv_tabBarController.tabBarHidden) {
        [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
    }
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.rdv_tabBarController.tabBarHidden) {
        [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
    }
}

#pragma mark - View init
-(void)p_initNavgationBar {
    self.title = @"订单详情";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImage *rightBarItemImage = [UIImage imageNamed:@"下线"];
    UIButton *rightBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBarButton setBackgroundImage:rightBarItemImage forState:UIControlStateNormal];
    [rightBarButton setTitle:@"我要接单" forState:UIControlStateNormal];
    rightBarButton.titleLabel.font = [UIFont boldSystemFontOfSize:13];
    rightBarButton.frame = CGRectMake(0, 0, 72, 22);
    [rightBarButton addTarget:self action:@selector(rightItemAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarButton];
    
    if (_type == OrderTypeEnumWait ||
        _type == OrderTypeEnumAuction) {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"放弃" style:UIBarButtonItemStylePlain target:self action:@selector(leftItemAction:)];
    }
}

- (void)p_initCollectionView {
    UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init];
    self.mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
    self.mainCollectionView.backgroundColor = [UIColor whiteColor];
    self.mainCollectionView.delegate = self;
    self.mainCollectionView.dataSource = self;
    //设置contentInset属性（上左下右的值）
    self.mainCollectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.view addSubview:self.mainCollectionView];
}

#pragma mark - Private Action
- (void)rightItemAction:(UIButton *)item {
    NSLog(@"right item clicked");
}

- (void)leftItemAction:(UIBarButtonItem *)item {
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"是否放弃该订单"
                                                                   message:@""
                                                            preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定"
                                                            style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {
                                                              [self.navigationController popViewControllerAnimated:YES];
                                                          }];
    
    [alert addAction:defaultAction];
    [self presentViewController:alert animated:YES completion:nil];
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (0 == section) {
        return 2;
    }
    else if (1 == section) {
        return 1;
    }
    else {
        return 4;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            static NSString *ID = @"NewOrderDetailInfoCellIdentifer";
            BCNewOrderDetailInfoCollectionViewCell *cell = [BCNewOrderDetailInfoCollectionViewCell cellWithCollectionView:collectionView];
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
            return cell;
        }
        else {
            static NSString *ID = @"MineGaryNullCellIdentifer";
            BCGaryNullCollectionViewCell *cell = [BCGaryNullCollectionViewCell cellWithCollectionView:collectionView];
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
            return cell;
        }
    }
    else if (indexPath.section == 1) {
        static NSString *ID = @"NewOrderStuffTitleCellIdentifer";
        BCNewOrderStuffTitleCollectionViewCell *cell = [BCNewOrderStuffTitleCollectionViewCell cellWithCollectionView:collectionView];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        return cell;
    }
    else {
        static NSString *ID = @"NewOrderStuffCellIdentifer";
        BCNewOrderStuffCollectionViewCell *cell = [BCNewOrderStuffCollectionViewCell cellWithCollectionView:collectionView];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        if (_type != OrderTypeEnumWait) {
            cell.addButton.hidden = YES;
            cell.decButton.hidden = YES;
            cell.priceTextFeild.userInteractionEnabled = NO;
        }
        return cell;
    }
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{
    UIEdgeInsets top = {0,0,0,0};
    return top;
}

//定义每个UICollectionViewCell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return CGSizeMake(self.view.frame.size.width, 146);
        }
        else {
            return CGSizeMake(self.view.frame.size.width, 10);
        }
    }
    else if (indexPath.section == 1) {
        return CGSizeMake(self.view.frame.size.width, 36);
    }
    else {
        return CGSizeMake(self.view.frame.size.width, 78);
    }
}

//每个item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0;
}

//collectionview点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
}

@end
